package com.cgsd.heyu.ui.home;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.viewpager2.widget.ViewPager2;

import com.android.library.utils.LogUtils;
import com.cgsd.heyu.R;
import com.cgsd.heyu.adapter.Vp2FragAdapter;
import com.cgsd.heyu.entity.AppUpdateEntity;
import com.cgsd.heyu.entity.CaoGen;
import com.cgsd.heyu.entity.TabEntity;
import com.cgsd.heyu.presenter.home.HomeContract;
import com.cgsd.heyu.presenter.home.HomePresenter;
import com.cgsd.heyu.ui.BaseActivity;
import com.cgsd.heyu.ui.BaseFragment;
import com.cgsd.heyu.ui.home.fragment.BrandFragment;
import com.cgsd.heyu.ui.home.fragment.CenterFragment;
import com.cgsd.heyu.ui.home.fragment.TaskAdFragment;
import com.cgsd.heyu.ui.home.fragment.TaskFragment;
import com.cgsd.heyu.ui.home.fragment.TriListFragment;
import com.cgsd.heyu.utils.StatusBarUtil;
import com.cgsd.heyu.widgets.CustomHomeTab;
import com.cgsd.heyu.widgets.dialog.AppUpdateDialog;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;

/**
 * @author xiaoxing
 * 主界面
 */
@Deprecated
public class MainActivity extends BaseActivity<HomeContract.Presenter>
        implements CustomHomeTab.HomeTabListener, HomeContract.MainView, LocationListener {
    /**
     * 跳转到草根时代界面
     */
    public static final String PHOTOGRAPH = "photograph";
    private CustomHomeTab mCustomHomeTab;
    private ViewPager2 mViewPage;
    private Vp2FragAdapter fragAdapter;
    private List<BaseFragment> fragments = new ArrayList<>();
    private AppUpdateDialog appUpdateDialog;


    public static void actionStart(Activity activity) {
        actionStart(activity, null);
//        activity.startActivity(TabActivity.getIntent(activity));
    }

    public static void actionStart(Activity activity, String type) {
        Intent intent = new Intent(activity, MainActivity.class);
        intent.putExtra("type", type);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        new HomePresenter(this);
        mCustomHomeTab = findViewById(R.id.mHomeTab);
        mViewPage = findViewById(R.id.mViewPager);
        mViewPage.setUserInputEnabled(false);

        mCustomHomeTab.setHomeTabListener(this);
        mViewPage.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                mCustomHomeTab.setSelectPosition(position);
            }
        });
    }

    @Override
    protected void initData() {
        mPresenter.getQiniuyunImageToken();
        mPresenter.appUpdate();
//        initTab(false);
        getTableSchema();

    }

    /**
     * init tab
     *
     * @param isTask
     */
    private void initTab(boolean isTask) {
        fragments.add(new TaskAdFragment());//首页发现
        fragments.add(new TriListFragment());//视频
        if (isTask) {
            fragments.add(TaskFragment.newInstance());//任务
        } else {
            fragments.add(new BrandFragment());//任务
        }
        fragments.add(new CenterFragment());//我
        fragAdapter = new Vp2FragAdapter(this, fragments);
        mViewPage.setAdapter(fragAdapter);
    }

    @Override
    public void clickTabPosition(int position, TabEntity tabEntity) {
        mViewPage.setCurrentItem(position);
    }

    @Override
    protected void setStatusBar() {
        StatusBarUtil.fullBar(this);
    }

    @Override
    public void onLocationChanged(@NonNull Location location) {
        Log.d("print", location.toString());
    }

    // todo
    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }
    // todo
    @Override
    public void onProviderEnabled(String s) {

    }
    // todo
    @Override
    public void onProviderDisabled(String s) {

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        String type = intent.getStringExtra("type");
        if (PHOTOGRAPH.equals(type)) {
            mViewPage.setCurrentItem(1);
        }
    }

    @Override
    public void setConfig(String sysType) {
    }

    @Override
    public void setAppUpdate(AppUpdateEntity appUpdateEntity) {
        if (appUpdateEntity == null) {
            return;
        }
        appUpdateDialog = new AppUpdateDialog(this, appUpdateEntity);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (appUpdateDialog != null) {
            appUpdateDialog.dismiss();
        }
    }

    public boolean videoIsShow() {
        return mViewPage.getCurrentItem() == 1;
    }

    /**
     * 获取指定表的表结构信息
     *
     * @return void
     * @throws
     * @method getTableSchema
     */
    public void getTableSchema() {
        BmobQuery<CaoGen> mBmobQuery = new BmobQuery<>();
        mBmobQuery.getObject("1j29LLLB", new QueryListener<CaoGen>() {
            @Override
            public void done(CaoGen caoGen, BmobException e) {
                if (e == null) {
                    LogUtils.i("sky", "查询成功==" + caoGen.toString());
                    initTab(caoGen.isTask());
//                    showToast("Config error tip！");
                } else {
                    LogUtils.i("sky", "查询失败==" + e.toString());
                    showToast("Config error tip！");
                    initTab(false);
                }
            }
        });

    }
}